package cn.micro.tree.bean;

import cn.micro.tree.function.NodeConvertToT;

import java.util.List;
import java.util.stream.Collectors;

public class TTree<T> extends Tree {

    private final NodeConvertToT<T> convert;

    private T t;

    public TTree(TreeNode treeNode, NodeConvertToT<T> convert) {
        super(treeNode);
        this.convert = convert;
    }

    @Override
    public void setRgt(Integer rgt) {
        super.setRgt(rgt);
        this.t = this.convert.convert(this);
        List<Tree> trees = super.getChildren();
        if (null != trees) {
            this.convert.children(t, trees.stream().map(convert::convert).collect(Collectors.toList()));
        }
    }

    public T convertToT() {
        return t;
    }
}
